
#include "queue_packet.h"




static  queue_packet_t  av_queue;
static SDL_mutex *queue_mutex = NULL;



int queue_packet_init(void)
{
	int i = 0;

	log_i("video queue init.\n");

	for(i=0;i<MAX_QUEUE_SIZE_4K;i++)
	{
		av_queue.video_data[i] = NULL;
		av_queue.video_size[i] = 0;
		av_queue.video_pts[i]  = 0; 
		av_queue.video_dts[i]  = 0; 

		av_queue.audio_data[i] = NULL;
		av_queue.audio_size[i] = 0;	
		av_queue.audio_pts[i]  = 0;  
	}

    av_queue.video_rindex = 0;	
    av_queue.video_windex = 0;	
	
    av_queue.audio_rindex = 0;	
    av_queue.audio_windex = 0;	

	queue_mutex = SDL_CreateMutex();

	return 0;
}

int queue_packet_write_video(uint8_t *data, int size,int64_t pts,int64_t dts)
{
	if(!data){
		log_e("data error\n");
		return -1;
	}
	
	//SDL_LockMutex(queue_mutex);

	av_queue.video_data[av_queue.video_windex] = malloc(size);

	if(av_queue.video_data[av_queue.video_windex] == NULL){
		log_e("malloc error\n");
		return -1;
	}

	memcpy(av_queue.video_data[av_queue.video_windex],data,size);
	av_queue.video_size[av_queue.video_windex] = size;
	av_queue.video_pts[av_queue.video_windex]  = pts;
	av_queue.video_dts[av_queue.video_windex]  = dts;

	av_queue.video_windex++;
	if(av_queue.video_windex >= MAX_QUEUE_SIZE_4K)
	{
		log_e("video queue full.\n");

		av_queue.video_windex = 0; 
	}

	//SDL_UnlockMutex(queue_mutex);

	return 0;

}


int queue_packet_read_video(uint8_t **packet,int* size,int64_t * pts,int64_t * dts)
{	
	*size   = 0;
	*packet = NULL;
	*pts    = 0;
	*dts    = 0;

	if(av_queue.video_rindex == av_queue.video_windex)
	{
		return -1;
	}

	*size   = av_queue.video_size[av_queue.video_rindex];
	*packet = av_queue.video_data[av_queue.video_rindex];
	*pts    = av_queue.video_pts [av_queue.video_rindex];
	*dts    = av_queue.video_dts [av_queue.video_rindex];

	av_queue.video_rindex++;
	if(av_queue.video_rindex >= MAX_QUEUE_SIZE_4K)
	{
		av_queue.video_rindex = 0; 
	}
	
	return 0;
}

int queue_packet_read_free(void)
{
	int index = 0;

	if(av_queue.video_rindex <= 0)
		return -1;

	index = av_queue.video_rindex - 1;


	if(av_queue.video_data[index])
	{
		free(av_queue.video_data[index]);
		av_queue.video_data[index] = NULL;
	}


	return 0;
}


int queue_packet_video_is_empty(void)
{
	if(av_queue.video_rindex == av_queue.video_windex)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


int queue_packet_write_audio(uint8_t *data, int size,int64_t pts)
{
	if(!data){
		log_e("data error\n");
		return -1;
	}

	av_queue.audio_data[av_queue.audio_windex]= malloc(size);
	if(av_queue.audio_data[av_queue.audio_windex] == NULL){
		log_e("malloc error\n");
		return -1;
	}
	
	//log_i("audio_windex = %d\n",av_queue.audio_windex);

	memcpy(av_queue.audio_data[av_queue.audio_windex],data,size);
	av_queue.audio_size[av_queue.audio_windex] = size;
	av_queue.audio_pts[av_queue.audio_windex]  = pts;

	av_queue.audio_windex++;
	if(av_queue.audio_windex >= MAX_QUEUE_SIZE_4K)
	{
		av_queue.audio_windex = 0; 
	}

	return 0;

}


int queue_packet_read_audio(uint8_t **packet,int* size,int64_t* pts)
{	
	*size	= 0;
	*packet = NULL;
	*pts    = 0;

	if(av_queue.audio_rindex == av_queue.audio_windex)
	{
		return -1;
	}

	*size	= av_queue.audio_size[av_queue.audio_rindex];
	*packet = av_queue.audio_data[av_queue.audio_rindex];
	*pts    = av_queue.audio_pts[av_queue.audio_rindex];

	//log_i("size = %d\n",*size);
	//log_i("packet = %x\n",*packet);

	av_queue.audio_rindex++;
	if(av_queue.audio_rindex >= MAX_QUEUE_SIZE_4K)
	{
		av_queue.audio_rindex = 0; 
	}
	
	return 0;
}

int queue_packet_read_audio_free(void)
{	
	int index = 0;

	if(av_queue.audio_rindex <= 0)
		return -1;

	index = av_queue.audio_rindex - 1;

	if(av_queue.audio_data[index])
	{
		free(av_queue.audio_data[index]);
		av_queue.audio_data[index] = NULL;
	}

	
	return 0;
}


int queue_packet_query_info(void)
{

	log_i("video_windex =%d\n",av_queue.video_windex);
	log_i("video_rindex =%d\n",av_queue.video_rindex);

	log_i("audio_windex =%d\n",av_queue.audio_windex);
	log_i("audio_rindex =%d\n",av_queue.audio_rindex);


	return 0;
}



int queue_packet_deinit(void)
{
	int i = 0;

	log_i("video queue exit.\n");

    av_queue.video_rindex = 0;	
    av_queue.video_windex = 0;	
	
    av_queue.audio_rindex = 0;	
    av_queue.audio_windex = 0;	


	for(i=0;i<MAX_QUEUE_SIZE_4K;i++)
	{
		if(av_queue.video_data[i])
		{
			free(av_queue.video_data[i]);
			av_queue.video_data[i] = NULL;
		}
		av_queue.video_size[i] = 0;

		if(av_queue.audio_data[i])
		{
			free(av_queue.audio_data[i]);
			av_queue.audio_data[i] = NULL;
		}
		av_queue.audio_size[i] = 0;	
		av_queue.audio_pts[i] = 0;	
	}



	return 0;
}













